package com.hnl.timer;

import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hnl.core.HNLConfig;
import com.hnl.core.HNLCore;
import com.hnl.core.TimerConfig;

public class HNLSOLRTasksTimerInitializer extends SOLRTasksTimer {
	static final Logger LOG = LoggerFactory.getLogger(HNLSOLRTasksTimerInitializer.class);

	private HNLSOLRTasksTimerInitializer(String url, String param, String interval) {
		super(url, param, interval);
	}

	public static void initHNLSOLRTasksTimer() {
		NodeList elems = HNLCore.getHNLConfig().getDoc()
				.getElementsByTagName(HNLConfig.CONFIG);
		TimerConfig timerConfig = new TimerConfig();
		try {
			timerConfig.readSOLRTimerFromXML((Element) elems.item(0));
		} catch (Exception e) {
			LOG.error("Timer initialization failed", e);
		}

		for (Map.Entry<String, Properties> entry : timerConfig.solrTimer
				.entrySet()) {
			Properties prop = entry.getValue();
			String interval = prop.getProperty("interval");
			String url = prop.getProperty("url");
			String param = prop.getProperty("param");
			new HNLSOLRTasksTimerInitializer(url, param, interval).init();
		}
	}
}
